define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'activity_data/index' + location.search,
                    add_url: 'activity_data/add',
                    edit_url: 'activity_data/edit',
                    del_url: 'activity_data/del',
                    multi_url: 'activity_data/multi',
                    import_url: 'activity_data/import',
                    dragsort_url: '',
                    table: 'activity_data',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                  pk: 'id',
                sortName: 'weigh',
                searchFormVisible: true,
                searchFormTemplate: 'customformtpl',
                // commonSearch: false,
                search:false,
                showToggle: false,
                showColumns: false,
                visible: false,
              
                pageSize: 10,
                showExport: true,
                columns: [
                    [
                      {checkbox: true},
                        {field: 'id', title: __('Id'), operate: false},
                        {field: 'area_name', title: '所屬區域', operate: false},
                        {field: 'images', title: __('封面'), operate: false, events: Table.api.events.image, formatter: Table.api.formatter.images},
                        {field: 'title', title: __('活動標題'), operate: 'LIKE'},
                        {field: 'total_hours', title: '總義工時數', sortable: true},
                        {field: 'start_date', title: __('活動開始時間'), operate:false, addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                          {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, formatter:function(value,row,index){
                            this.buttons = [
                             
                               
                                   {
                                    name: 'colunteer_list',
                                    text: '義工名稱',
                                    classname: 'btn btn-xs btn-success btn-magic btn-dialog',
                                    url: 'activity/volunteer_list',
                                    // confirm: '确认禁用嗎？',
                                    visible: function (row) {
                                        // if ( row.level == 2 &&  Config.area_staff) {
                                        //     return true;
                                        // }else{
                                        //     return false;
                                        // }
                                        return true;
                                        //返回true时按钮显示,返回false隐藏
                                    },
                                    success: function () {
                                        $(".btn-refresh").trigger("click");
                                    },
                                    error: function () {

                                    },
                                    refresh: true,
                                },
                                  {
                                    name: 'edit',
                                    text: '簽到',
                                    title: '簽到',
                                    classname: 'btn btn-xs btn-info btn-dialog',
                                    icon: '',
                                    url: 'activity_data/edit',
                                    visible: function (row) {
                                        //返回true时按钮显示,返回false隐藏
                                        // if ( Config.activity_edit ) {
                                        //     return true;
                                        // }else{
                                        //     return false;
                                        // }
                                         return true;
                                    }
                                },
                              
                                
                                ];
                                return Table.api.formatter.buttons.call(this,value,row,index)
                            }
                        }
                    ]
                ]
            });

                  table.on("pre-body.bs.table",function(){
                var bt=table.data("bootstrap.table");
                if (bt){
                    bt.$toolbar.find(".export").find(".icon-share").text("導出");
                    bt.$toolbar.find(".export").find(".icon-share").removeClass("glyphicon glyphicon-export icon-share");
                }
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },

             add: function () {
            $(document).on('click','.spec_add_btn', function (event) {
                var user_ids = '';
                $('#tag_div .tag').each(function(){
                    user_ids =  user_ids+ $(this).attr('attr-id')+',';
                })
                var reg=/,$/gi;//此处是正则
                user_ids=user_ids.replace(reg,"");
                var url = $(this).attr('data-url')+'/ids/'+user_ids;
                if(!url) return false;
                var msg = $(this).attr('data-title');
                var width = $(this).attr('data-width');
                var height = $(this).attr('data-height');
                var area = [$(window).width() > 800 ? (width?width:'800px') : '65%', $(window).height() > 600 ? (height?height:'600px') : '65%'];
                var options = {
                    shadeClose: false,
                    shade: [0.3, '#393D49'],
                    area: area,
                    callback:function(data){

                        $("#tag_div").html('');
                        $.each(data,function(index,value){
                            Controller.selectTag(value)
                        });
                        // CallBackFun(value.id, value.name);//在回调函数里可以调用你的业务代码实现前端的各种逻辑和效果
                    }
                };
                Fast.api.open(url,msg,options);
            });
            $(document).on('click','.spec_adds_btn', function (event) {
                var user_ids = '';
                $('#tag_divs .tags').each(function(){
                    user_ids =  user_ids+ $(this).attr('attr-id')+',';
                })
                console.log(user_ids);
                var reg=/,$/gi;//此处是正则
                user_ids=user_ids.replace(reg,"");
                var url = $(this).attr('data-url')+'/ids/'+user_ids;
                if(!url) return false;
                var msg = $(this).attr('data-title');
                var width = $(this).attr('data-width');
                var height = $(this).attr('data-height');
                var area = [$(window).width() > 800 ? (width?width:'800px') : '65%', $(window).height() > 600 ? (height?height:'600px') : '65%'];
                var options = {
                    shadeClose: false,
                    shade: [0.3, '#393D49'],
                    area: area,
                    callback:function(data){

                        $("#tag_divs").html('');
                        $.each(data,function(index,value){
                            Controller.selectTags(value)
                        });
                        // CallBackFun(value.id, value.name);//在回调函数里可以调用你的业务代码实现前端的各种逻辑和效果
                    }
                };
                Fast.api.open(url,msg,options);
            });
            Controller.api.bindevent();
        },
        recyclebin: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    'dragsort_url': ''
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: 'activity_data/recyclebin' + location.search,
                pk: 'id',
                sortName: 'id',
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id')},
                        {field: 'name', title: __('Name'), align: 'left'},
                        {
                            field: 'deletetime',
                            title: __('Deletetime'),
                            operate: 'RANGE',
                            addclass: 'datetimerange',
                            formatter: Table.api.formatter.datetime
                        },
                        {
                            field: 'operate',
                            width: '130px',
                            title: __('Operate'),
                            table: table,
                            events: Table.api.events.operate,
                            buttons: [
                                {
                                    name: 'Restore',
                                    text: __('Restore'),
                                    classname: 'btn btn-xs btn-info btn-ajax btn-restoreit',
                                    icon: 'fa fa-rotate-left',
                                    url: 'activity_data/restore',
                                    refresh: true
                                },
                                {
                                    name: 'Destroy',
                                    text: __('Destroy'),
                                    classname: 'btn btn-xs btn-danger btn-ajax btn-destroyit',
                                    icon: 'fa fa-times',
                                    url: 'activity_data/destroy',
                                    refresh: true
                                }
                            ],
                            formatter: Table.api.formatter.operate
                        }
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },

        // add: function () {
        //     Controller.api.bindevent();
        // },
        edit: function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});